// src/main/java/com/example/coursehub/repository/CourseStudentRepository.java
package com.example.coursehub.repository;

import com.example.coursehub.entity.CourseStudent;
import com.example.coursehub.entity.CourseStudentId;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;


public interface CourseStudentRepository extends JpaRepository<CourseStudent, CourseStudentId>, JpaSpecificationExecutor<CourseStudent> {
    List<CourseStudent> findByCourse_CourseId(String courseId);
    List<CourseStudent> findByUser_UserId(String userId);
    Optional<CourseStudent> findByCourse_CourseIdAndUser_UserId(String courseId, String userId);
    void deleteByCourse_CourseIdAndUser_UserId(String courseId, String userId);


    boolean existsById_courseIdAndId_userId(String courseId, String userId);

    @Query("SELECT EXISTS(SELECT cs FROM CourseStudent cs WHERE cs.id.courseId = :courseId AND cs.id.userId = :userId)")
    boolean existsByCourseIdAndUserId(@Param("courseId") String courseId, @Param("userId") String userId);
}